Public Class frmTeam

    Private _myTeamId As Integer

    Dim myCoach As coachingAppObjects.rw_coachRoot

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim myTeam As coachingAppObjects.rw_teamRoot
        myTeam = coachingAppObjects.rw_teamRoot.NewTeamRoot(0)
        myTeam.CoachId = 2
        myTeam.DivisionId = 2
        myTeam.PlayerCount = 0
        myTeam.TeamName = "Test"
        myTeam.TeamNumber = 0
        myTeam.Save()



        'MessageBox.Show(pnlPlayers.Controls.Item("txtPlayerFirst0").Text)
        'Dim myTeam As coachingAppObjects.rw_teamRoot
        'myTeam = coachingAppObjects.rw_teamRoot.NewTeamRoot(0)

        'Dim myPlayer As coachingAppObjects.rw_playerChild
        'myPlayer = coachingAppObjects.rw_playerChild.NewPlayerChild

        'Dim myplayerList As coachingAppObjects.rw_playerChildList
        'myplayerList = coachingAppObjects.rw_playerChildList.NewPlayerChildList


        'myTeam.CoachId = 0
        'myTeam.DivisionId = 0
        'myTeam.PlayerCount = 0
        'myTeam.TeamName = ""
        'myTeam.TeamNumber = 0




    End Sub

    'Private Sub cboPlayerCount_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    pnlPlayers.Controls.Clear()

    '    Dim i As Integer = 0
    '    Dim iLocation As Integer = 1
    '    Dim iCount As Integer = CType(cboPlayerCount.Text, Integer)

    '    For i = 0 To iCount - 1
    '        Dim playerLabel As New Windows.Forms.Label
    '        Dim playerTextBoxFirst As New Windows.Forms.TextBox
    '        Dim playerTextBoxLast As New Windows.Forms.TextBox

    '        If i = 0 Then
    '            playerLabel.Location = New Point(125, iLocation)
    '            playerTextBoxFirst.Location = New Point(190, 1)
    '            playerTextBoxLast.Location = New Point(315, 1)
    '        Else
    '            iLocation = iLocation + 26
    '            playerLabel.Location = New Point(125, iLocation)
    '            playerTextBoxFirst.Location = New Point(190, iLocation)
    '            playerTextBoxLast.Location = New Point(315, iLocation)
    '        End If

    '        playerLabel.Size = New Point(48, 13)
    '        playerLabel.Text = "Player #" & i + 1 & ":"
    '        playerLabel.AutoSize = True
    '        pnlPlayers.Controls.Add(playerLabel)

    '        playerTextBoxFirst.Name = "txtPlayerFirst" & i
    '        playerTextBoxLast.Name = "txtPlayerLast" & i
    '        pnlPlayers.Controls.Add(playerTextBoxFirst)
    '        pnlPlayers.Controls.Add(playerTextBoxLast)



    '    Next
    'End Sub

    Private Sub frmTeam_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadCoaches()
        Dim myPlayers As coachingAppObjects.rw_playerChildList
        myPlayers = coachingAppObjects.rw_playerChildList.NewPlayerChildList



    End Sub

    Private Sub cboCoach_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCoach.SelectedIndexChanged
        If cboCoach.Text <> "Please Select" Then
            loadDivisions()
        End If
    End Sub

    Private Sub loadCoaches()
        cboCoach.Items.Clear()

        Dim myCoachList As coachingAppObjects.ro_coachRootList
        Dim item As coachingAppObjects.ro_coachChild

        myCoachList = coachingAppObjects.ro_coachRootList.Getro_coachRootList
        cboCoach.Items.Add("Please Select")
        For Each item In myCoachList
            cboCoach.Items.Add(item)
        Next
        cboCoach.SelectedIndex = 0
    End Sub
    Private Sub loadDivisions()

        myCoach = coachingAppObjects.rw_coachRoot.GetCoachRoot(CType(cboCoach.SelectedItem, coachingAppObjects.ro_coachChild).CoachId)
        Dim item As coachingAppObjects.ro_divisionChild

        'Get DivisionList
        Dim myDivisionList As coachingAppObjects.ro_divisionRootList
        myDivisionList = coachingAppObjects.ro_divisionRootList.GetDivisionRootList(myCoach.LeagueId)

        cboDivisions.Items.Clear()
        cboDivisions.Items.Add("Please Select")
        For Each item In myDivisionList
            cboDivisions.Items.Add(item)
        Next
        cboDivisions.SelectedIndex = 0
    End Sub


    Private Sub cboDivisions_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDivisions.SelectedIndexChanged
        If cboDivisions.Text <> "Please Select" Then
            Dim myDivision As coachingAppObjects.rw_divisionRoot
            myDivision = coachingAppObjects.rw_divisionRoot.GetDivisionRoot(CType(cboDivisions.SelectedItem, coachingAppObjects.ro_divisionChild).DivisionId, myCoach.LeagueId)
            Try
                Dim myTeam As coachingAppObjects.rw_teamRoot
                myTeam = coachingAppObjects.rw_teamRoot.NewTeamRoot(0)
                myTeam.CoachId = myCoach.CoachId
                myTeam.DivisionId = myDivision.DivisionId
                myTeam.TeamNumber = 0
                myTeam.TeamName = "Newly Created Team"
                myTeam.PlayerCount = 0
                myTeam.Save()

                _myTeamId = myTeam.TeamId
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

    Private Sub txtTeamNumber_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTeamNumber.Leave
        Dim myTeam As coachingAppObjects.rw_teamRoot
        myTeam = coachingAppObjects.rw_teamRoot.GetTeamRoot(_myTeamId)
        myTeam.TeamNumber = txtTeamNumber.Text
        myTeam.Save()
    End Sub

    Private Sub txtTeamName_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTeamName.Leave
        Dim myTeam As coachingAppObjects.rw_teamRoot
        myTeam = coachingAppObjects.rw_teamRoot.GetTeamRoot(_myTeamId)
        myTeam.TeamName = txtTeamName.Text
        myTeam.Save()
    End Sub

    Private Sub txtPlayerAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPlayerAdd.Click

        Dim myTeam As coachingAppObjects.rw_teamRoot
        myTeam = coachingAppObjects.rw_teamRoot.GetTeamRoot(_myTeamId)

        Dim myPlayer As coachingAppObjects.rw_playerChild
        myPlayer = coachingAppObjects.rw_playerChild.NewPlayerChild

        myPlayer.PlayernameFirst = txtFirstName.Text
        myPlayer.PlayernameLast = txtLastName.Text
        Try
            myTeam.players.Add(myPlayer)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


        myTeam.Save()
        getCount()
        txtFirstName.Clear()
        txtLastName.Clear()

        txtFirstName.Focus()
    End Sub

    Private Sub getCount()
        Dim myTeam As coachingAppObjects.rw_teamRoot
        myTeam = coachingAppObjects.rw_teamRoot.GetTeamRoot(_myTeamId)
        Me.txtPlayerCount.Text = myTeam.PlayerCount
        myTeam = Nothing
    End Sub
    Private Sub llShowPlayerList_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llShowPlayerList.LinkClicked
        getCount()
        pnlPlayers.Controls.Clear()

        Dim myTeam As coachingAppObjects.rw_teamRoot
        myTeam = coachingAppObjects.rw_teamRoot.GetTeamRoot(_myTeamId)
        Dim player As coachingAppObjects.rw_playerChild

        Dim i As Integer = 0
        Dim iLocation As Integer = 1
        Dim iCount As Integer = CType(txtPlayerCount.Text, Integer)

        For Each player In myTeam.players

            Dim playerLabel As New Windows.Forms.Label
            Dim playerTextBoxFirst As New Windows.Forms.TextBox
            Dim playerTextBoxLast As New Windows.Forms.TextBox

            If i = 0 Then
                playerLabel.Location = New Point(125, iLocation)
                playerTextBoxFirst.Location = New Point(190, 1)
                playerTextBoxLast.Location = New Point(315, 1)
            Else
                iLocation = iLocation + 26
                playerLabel.Location = New Point(125, iLocation)
                playerTextBoxFirst.Location = New Point(190, iLocation)
                playerTextBoxLast.Location = New Point(315, iLocation)
            End If

            playerLabel.Size = New Point(48, 13)
            playerLabel.Text = "Player #" & i + 1 & ":"
            playerLabel.AutoSize = True
            pnlPlayers.Controls.Add(playerLabel)

            playerTextBoxFirst.Name = "txtPlayerFirst" & i
            playerTextBoxFirst.Text = player.PlayernameFirst
            playerTextBoxLast.Name = "txtPlayerLast" & i
            playerTextBoxLast.Text = player.PlayernameLast
            pnlPlayers.Controls.Add(playerTextBoxFirst)
            pnlPlayers.Controls.Add(playerTextBoxLast)

            i += 1
        Next




    End Sub
End Class